package net.slavics.maps.impl;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import javax.imageio.ImageIO;
import net.slavics.images.Images;
import net.slavics.maps.ifc.IGeoTransfer;
import net.slavics.maps.ifc.IMapa;
import net.slavics.maps.ifc.MapComponent;
import org.apache.log4j.Logger;

/**
 *
 * @author slavic01
 * @version $Revision: 22 $
 * $LastChangedDate: 2008-05-05 19:27:06 +0000 (Mon, 05 May 2008) $ $Author: pslavicek@seznam.cz $
 */
public abstract class Maps implements IMapa {

    private static Logger _log = Logger.getLogger(Maps.class);
    protected int width = 0;
    protected int height = 0;
    protected int minX = 0;
    protected int maxX = 0;
    protected int minY = 0;
    protected int maxY = 0;
    protected IGeoTransfer geoTransfer;
    protected Proxy proxy = null;
    protected MapComponent owner;

    private Maps() {
    }

    /**
     * Nastaví rozměr mapy
     * @param width
     * @param height
     */
    protected Maps(int width, int height) {
        this.width = width;
        this.height = height;
    }

    protected Maps(int width, int height, int minx, int miny, int maxx, int maxy) {
        this.width = width;
        this.height = height;
        this.minX = minx;
        this.minY = miny;
        this.maxX = maxx;
        this.maxY = maxy;
    }

    protected void setMaxXY(int x, int y) {
        this.maxX = x;
        this.maxY = y;
    }

    protected void setMinXY(int x, int y) {
        this.minX = x;
        this.minY = y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    protected void savePicture(File file, BufferedImage im) throws IOException {
        try {
            ImageIO.write(im, "png", file);
        } catch (FileNotFoundException ex) {
            _log.warn("Soubor " + file.getAbsolutePath() + " neuložen " + ex.getLocalizedMessage());
        } catch (Exception ex) {
            _log.error("Soubor " + file.getAbsolutePath() + " neuložen " + ex.getLocalizedMessage());
        }
    }

    protected BufferedImage getImageFromUrl(URL url) throws IOException {
        BufferedImage img = null;
        _log.trace(url.getHost() + " " + url.getPath());
        img = Images.getImage(url, proxy);
        _log.info("Load " + url.getPath());
        return img;
    }

    protected BufferedImage getImageFromFile(File file) throws IOException {
        BufferedImage img = null;
        _log.trace(file.exists() + " " + file.getAbsolutePath());
        img = ImageIO.read(new FileInputStream(file));
        _log.info("Load " + file.getAbsolutePath());
        return img;
    }

    public Point getMinXY() {
        return new Point(minX, minY);
    }

    public Point getMaxXY() {
        return new Point(maxX, maxY);
    }

    public Point getCenterXY() {
        return new Point((maxX - minX) / 2 + minX, (maxY - minY) / 2 + minY);
    }

    public IGeoTransfer getGeoTransfer() {
        return geoTransfer;
    }

    public void setOwner(MapComponent owner) {
        this.owner = owner;
        proxy = owner.getProxy();
    }
}
